﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.IO;
using System.Web.Compilation;
using System.Web.WebPages;

namespace V5_EShop {
    public partial class Index : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string pageName = "index";
                string rPageName = InputHelper.SetString(Request["t"]);
                if (!string.IsNullOrEmpty(rPageName)) {
                    pageName = rPageName;
                }
                string templateDir = "default";
                if (!string.IsNullOrEmpty(pageName)) {
                    templateDir = V5.Config.View["TemplatePath"].ToString();
                    pageName = V5.Url.SplitController(pageName);
                    string PathPage = "/Template/" + templateDir + "/" + pageName + ".cshtml";

                    var path = System.IO.Path.Combine(HttpContext.Current.Request.ApplicationPath, PathPage);
                    var type = BuildManager.GetCompiledType(path);
                    var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
                    var stringWriter = new StringWriter();
                    script.VirtualPath = "~" + PathPage;
                    script.ExecutePageHierarchy(new WebPageContext(
                            new HttpContextWrapper(HttpContext.Current), script, null
                            ), stringWriter);

                    Response.Write(stringWriter.ToString());
                    Response.End();
                }
            }
        }

        protected void Page_Error(object sender, EventArgs e) {
            Exception ex = Server.GetLastError();
            if (ex is HttpRequestValidationException) {
                Response.Write(ex.Message + "==" + ex.InnerException);
                Server.ClearError();
            }
        }

    }
}